﻿using System;

namespace AladdinGameFramework.ObjectPool
{
	internal partial class ObjectPoolManager
	{
		/// <summary>
		/// 内部对象
		/// </summary>
		/// <typeparam name="T">对象类型</typeparam>
		private sealed class Object<T> where T : ObjectBase
		{
			private readonly T m_Object;
			private int m_SpawnCount;

			/// <summary>
			/// 初始化内部对象的新实例
			/// </summary>
			/// <param name="obj">对象</param>
			/// <param name="spawned">对象是否已被获取</param>
			public Object(T obj, bool spawned)
			{
				if (obj == null)
					throw new GameFrameworkException("Object is invalid.");
				m_Object = obj;
				m_SpawnCount = spawned ? 1 : 0;
				if (spawned)
					m_Object.OnSpawn();
			}

			/// <summary>
			/// 获取对象名称
			/// </summary>
			public string Name
			{
				get
				{
					return m_Object.Name;
				}
			}

			/// <summary>
			/// 获取对象是否被加锁
			/// </summary>
			public bool Locked
			{
				get
				{
					return m_Object.Locked;
				}
				internal set
				{
					m_Object.Locked = value;
				}
			}

			/// <summary>
			/// 获取对象优先级
			/// </summary>
			public int Priority
			{
				get
				{
					return m_Object.Priority;
				}
				internal set
				{
					m_Object.Priority = value;
				}
			}

			/// <summary>
			/// 获取对象上次使用时间
			/// </summary>
			public DateTime LastUseTime
			{
				get
				{
					return m_Object.LastUseTime;
				}
			}

			/// <summary>
			/// 获取对象是否正在使用
			/// </summary>
			public bool IsInUse
			{
				get
				{
					return m_SpawnCount > 0;
				}
			}

			/// <summary>
			/// 获取对象的获取计数
			/// </summary>
			public int SpawnCount
			{
				get
				{
					return m_SpawnCount;
				}
			}

			/// <summary>
			/// 查看对象
			/// </summary>
			/// <returns>对象</returns>
			public T Peek()
			{
				return m_Object;
			}

			/// <summary>
			/// 获取对象
			/// </summary>
			/// <returns>对象</returns>
			public T Spawn()
			{
				m_SpawnCount++;
				m_Object.LastUseTime = DateTime.Now;
				m_Object.OnSpawn();
				return m_Object;
			}

			/// <summary>
			/// 回收对象
			/// </summary>
			public void Unspawn()
			{
				m_Object.OnUnspawn();
				m_Object.LastUseTime = DateTime.Now;
				m_SpawnCount--;
				if (m_SpawnCount < 0)
					throw new GameFrameworkException("Spawn count is less than 0.");
			}

			/// <summary>
			/// 释放对象
			/// </summary>
			public void Release()
			{
				m_Object.Release();
			}
		}
	}
}
